package com.ganee.agora.view

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProviders
import com.alibaba.android.arouter.facade.annotation.Autowired
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.bumptech.glide.Glide
import com.bumptech.glide.load.engine.DiskCacheStrategy
import com.ganee.agora.R
import com.ganee.agora.model.InviteInfoEntity
import com.ganee.agora.viewmodel.DetailViewModel
import com.ganee.agora.viewmodel.SettingsViewModel
import com.ganee.baselib.arouter.Constance
import com.ganee.baselib.mvvmlibrary.base.BaseActivity
import kotlinx.android.synthetic.main.activity_invite.*
import kotlinx.android.synthetic.main.toolbar_with_title.*
import okhttp3.MediaType
import okhttp3.RequestBody
import org.json.JSONException
import org.json.JSONObject

@Route(path = Constance.ACTIVITY_URL_INVITE)
class InviteActivity : BaseActivity<DetailViewModel>() {

    @JvmField
    @Autowired(name = "activeId")
    var activeId: String? = null


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_invite)
        ARouter.getInstance().inject(this)
        initView()
        initHttpRequest()
    }

    private fun initHttpRequest() {
        val requestData = JSONObject()
        try {
            requestData.put("activeId", activeId)
        } catch (e: JSONException) {
            e.printStackTrace()
        }
        val requestBody = RequestBody.create(MediaType.parse("application/json"), requestData.toString())
        viewModel.getInviteInfo(requestBody)

        viewModel.inviteInfoEntity.observe(this, Observer<InviteInfoEntity> {
            initData(it)
        })
    }

    private fun initData(entity: InviteInfoEntity) {
        invite_tv_title.text = entity.eventTitle
        invite_tv_user.text = "hosted by ${entity.host}"
        invite_tv_location.text = entity.location
        var start:String? = ""
        var end:String? = ""
        if (null != entity.startTime) {
            start = getTime(entity.startTime)
        }
        if(null != entity.endTime){
            end = getTime(entity.endTime)
        }
        invite_tv_time.text = "$start - $end"

        Glide.with(this@InviteActivity)
                .load(entity.qrcodePath)
                .diskCacheStrategy(DiskCacheStrategy.ALL)
                .centerCrop()
                .dontAnimate()
                .thumbnail(0.5f)
                .into(invite_iv_qrcode)
    }

    private fun initView() {
        toolbarCancel.setOnClickListener{
            finish()
        }
        toolbarTitle.text = "Invite"
    }

    override fun getViewModel(): DetailViewModel {
        return ViewModelProviders.of(this@InviteActivity).get(DetailViewModel::class.java)
    }

    private fun getTime(time:String):String{
        val month = time.substring(4, 6).toInt()
        val day = time.substring(6, 8)
        val hour = time.substring(8, 10)
        val minute = time.substring(10, 12)
        var mMonth: String? = null
        when (month) {
            1 -> mMonth = "Jan"
            2 -> mMonth = "Feb"
            3 -> mMonth = "Mar"
            4 -> mMonth = "Apr"
            5 -> mMonth = "May"
            6 -> mMonth = "Jun"
            7 -> mMonth = "Jul"
            8 -> mMonth = "Aug"
            9 -> mMonth = "Sept"
            10 -> mMonth = "Oct"
            11 -> mMonth = "Nov"
            12 -> mMonth = "Dec"
        }
        var mHour: String? = null
        mHour = if (hour.toInt() >= 12) {
            hour + ":" + minute + "pm"
        } else {
            hour + ":" + minute + "am"
        }
        return "$mMonth $day · $mHour"
    }
}
